<template>
  <div class="container">
    <!-- 头部 -->
    <div class="header">
      <div class="h-left">
        <div class="choose fs-32" @click="goBack">取消</div>
      </div>
      <div class="h-center">
        <div class="title fs-36">{{title}}</div>
      </div>
      <div class="h-right">
        <div class="close fs-32" @click="saveChoose">完成</div>
      </div>
    </div>
    <!-- tips   提示 -->
    <div class="tip-mes">请选择合适的标签，最多可选择3个</div>
    <!-- 标签列表 -->
    <div class="type-box" v-for="(item,index) in list" :key="index">
      <div class="title">
        <div class="img">
          <img src alt />
        </div>
        <span class="text fs-32">{{item.music_list_category_name}}</span>
      </div>
      <div class="list">
        <div
          class="items"
          v-for="(items,index) in item.son"
          :key="index"
          @click="checkTips(item.son,items,item)"
          :class="{active:items.isChecked && items.id == item.activeIndex}"
        >
          <span class="name">{{items.music_list_category_name}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "mall",
  data() {
    return {
      title: "选择标签",
      checkList: [
        {
          id: 1,
          name: "",
          status:0
        },
        {
          id: 2,
          name: "",
          status:0
        },
        {
          id: 3,
          name: "",
          status:0
        }
      ], //选择的标签 数组
      tips: "", //选择标签
      pics: [],
      list: [], //标签列表数据
      checkedId:[]
    };
  },
  components: {},
  methods: {
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    // 完成选择
    saveChoose() {
      this.tips = "";
      this.checkList.map(item => {
        if (item.name != "") {
          this.tips += item.name + " ";
          this.checkedId.push(item.status);
        }
      });
      window.sessionStorage.setItem('tips', JSON.stringify(this.tips));
      window.sessionStorage.setItem('tipsId', JSON.stringify(this.checkedId));
      this.$router.go(-1);
    },
    // 获取标签列表数据
    getTipData() {
      const url = this.$host1 + `home/chooseSongListCategory`;
      this.$axios
        .get(url)
        .then(res => {
          // console.log(res.data, "---res.data--");
          if (res.data.code == 200) {
            this.list = res.data.data.result;
            this.list.map(item => {
              item.avtiveIndex = 0;
            });
            this.list.son.map(item => {
              item.isChecked = false;
            });
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 选择标签
    checkTips(item, items, fa) {
      // item  表示每种类型子分类的数组    items  表示当前点击的对象  fa 表示当前点击的父分类
      item.map(res => {
        if (res.id == items.id) {
          res.isChecked = true;
          fa.activeIndex = items.id;
        } else {
          res.isChecked = false;
        }
        this.$forceUpdate();
      });
      let arr = item.filter(item => {
        return item.isChecked == true;
      });
      console.log(fa.id, "---fa.id");
      this.checkList.map(item => {
        if (item.id == fa.id) {
          item.name = arr[0].music_list_category_name;
          item.status = items.id;
        }
      });
    }
  },
  created() {
    this.getTipData();
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  padding-top: 1rem;
  box-sizing: border-box;
  .header {
    width: 100%;
    height: 1rem;
    padding: 0 0.34rem 0 0.31rem;
    box-sizing: border-box;
    background: linear-gradient(to right, #680dae, #9529c6);
    position: fixed;
    left: 0;
    top: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .h-left {
      .choose {
        font-weight: 500;
        color: rgba(255, 255, 255, 1);
        line-height: 0.32rem;
      }
    }
    .h-center {
      .title {
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
        line-height: 0.32rem;
      }
    }
    .h-right {
      .close {
        font-weight: 500;
        color: rgba(255, 255, 255, 1);
        line-height: 0.32rem;
      }
    }
  }

  .tip-mes {
    font-weight: 500;
    color: rgba(153, 153, 153, 1);
    font-size: 0.28rem;
    text-align: center;
    margin-top: 0.26rem;
    margin-bottom: 0.2rem;
  }

  .type-box {
    background-color: #fff;
    .title {
      height: 1rem;
      display: flex;
      justify-content: center;
      align-items: center;
      .img {
        // width: 0.4rem;
        // height: 0.4rem;
        // margin-right: 0.16rem;
        // background-color: pink;
        img {
          width: 100%;
        }
      }
      .text {
        font-weight: 500;
        color: rgba(153, 153, 153, 1);
      }
    }

    .list {
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 0.18rem;
      border-top: 0.015rem solid #e9e9e9;
      .items {
        width: 1.87rem;
        line-height: 0.92rem;
        height: 0.92rem;
        text-align: center;
        box-sizing: border-box;
        border-bottom: 0.015rem solid #e9e9e9;
        border-right: 0.015rem solid #e9e9e9;
      }
      .items:nth-child(4n) {
        border-right: none;
      }
      .active {
        color: #f44349;
        border: 0.015rem solid #f44349 !important;
      }
    }
  }
}
</style>
